<%-- 
    Document   : search
    Created on : Nov 14, 2010, 3:45:49 PM
    Author     : bertram
--%>

<%@page import="data.DataProvider"%>
<%@page import="entities.Artwork"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.Map"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ include file="banner.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <%try {
                        new data.Auth().checkAuth(request);
                    } catch (exceptions.AuthException aux) {
                        System.out.println("fejl: " +aux.getMessage());
                        response.sendRedirect("login.html");
                    }
        ArrayList<Integer> results = null;
        if(request.getAttribute("results")!=null)
            {
                results = (ArrayList<Integer>)request.getAttribute("results");
            }
        %>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Søgning i billeddeling.dk</title>
    </head>
    <body>
        <div id="main">
        <h1>Søg på eksisterende værker</h1><table border="0">
            <thead>
                <tr>
                    <th width="50%">Søgekriterier</th>
                    <th width="50%">Resultater</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <form name="searchterms" action="search.do" accept-charset="UTF-8" enctype="application/x-www-form-urlencoded;charset=UTF-8" method="POST">
                            <br />Titel: <br /><input type="text" name="titel" value="" size="50" value='<%=request.getParameter("titel")%>' />
                            <br />Kunstner: <br /><input type="text" name="kunstner" value="" size="50" value='<%=request.getParameter("kunstner")%>'/>
                            <br />Periode: <br />
                            start år: <input type="text" name="startaar" value="" size="4" />
                            slut år: <input type="text" name="slutaar" value="" size="4" />
                            <br /><input type="submit" value="Søg" name="btn" />
                        </form>
                    </td>
                    <td>
                        Du søgte med følgende parametre:<br />
                        <%
                        System.out.println("encoding: " + request.getCharacterEncoding());
                        Map<String,String[]> params = request.getParameterMap();
                        StringBuilder terms = new StringBuilder();
                        for(String name : params.keySet())
                            {
                               terms.append(name);
                                terms.append(": ");
                                String[] values = params.get(name);
                                for(int i = 0; i< values.length;i++)
                                    {
                                        terms.append(values[i] + ", ");
                                    }
                                terms.append("<br />");
                            }
                        %>
                        <%=terms%>
                        Resultater:<br />
                        <%
                            if(results != null){
                                if(results.isEmpty())
                                    {
                                        out.write("ingen resultater");
                                    }
                                else{
                                        DataProvider prov = new DataProvider();
                                        for(Integer i : results)
                                            {
                                                Artwork work = prov.getWork(i);
                                                out.println("<a href='showwork.jsp?vaerkID=" + i+ "'>"+work.getTitel()+"</a>");
                                                out.println("kunstner: " +work.getArtistString());
                                                out.println("<br />");

                                            }
                                    }
                                }
                        %>
                        
                    </td>
                </tr>
            </tbody>
        </table>
        </div>
    </body>
</html>
